using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Map
{
    public int rowNum;
    public int colNum;
    public MapCell[,] mapCells;
    public GameObject roadPrefab;
    public List<Vector2> roads = new List<Vector2>();
    public MapStatus status = MapStatus.UnInited;
    public LevelData data;
    public int nowBoShu = 1;
    public void initMapCell(LevelData nowLevel, Action callBack = null)
    {
        data = nowLevel;
        this.rowNum = nowLevel.row;
        this.colNum = nowLevel.col;
        Debug.Log($"row:{nowLevel.row} col:{nowLevel.col}");
        mapCells = new MapCell[rowNum, colNum];
        this.roads.Clear();
        this.roads.AddRange(nowLevel.roads);
        if (roads.Count > 1)
        {
            Debug.Log("路点数量："+roads.Count);
            for (var i = 0; i < roads.Count - 1; i++)
            {
                Vector2 road = roads[i];

                MapCell mapCell = new MapCell();
                mapCell.row = (int)road.x;
                mapCell.col = (int)road.y;
                mapCell.isRoad = true;
                mapCell.pos.x = -colNum / 2 + 0.5f + road.y;
                mapCell.pos.y = rowNum / 2 - 0.5f - road.x;
                // Debug.Log($"row:{mapCell.row} col:{mapCell.col}");
                this.mapCells[mapCell.row, mapCell.col] = mapCell;
                //TODO 传入的路点是关键点，和前一个点不是同行同列，需要计算中间的路点
                // if (i +1  == roads.Length)
                // {
                //     break;
                // }
                //
                // Vector2 one = roads[i];
                // Vector2 two = roads[i+1];
                // float colAdd = two.y - one.y;
                // if (colAdd != 0)//不在同已行
                // {
                //         
                // }
            }
        }
        else
        {
            throw new Exception("路点数量小于2，路径初始化失败！");
        }

        //
        for (int i = 0; i < rowNum; i++)
        {
            for (int j = 0; j < colNum; j++)
            {
                if (mapCells[i, j] != null)
                {
                    continue;
                }

                MapCell mapCell = new MapCell();
                mapCell.row = i;
                mapCell.col = j;
                mapCells[i, j] = mapCell;
                mapCell.pos.x = -colNum / 2 + 0.5f + j;
                mapCell.pos.y = rowNum / 2 - 0.5f - i;
            }
        }

        initRoads();

        if (callBack != null)
        {
            callBack.Invoke();
        }
        status = MapStatus.Inited;
    }

    private void initRoads()
    {
        GameObject roadParent = GameObject.FindWithTag("Road");
        //删除旧孩子
        var count = roadParent.transform.childCount;
        for (int i = 0; i < count; i++)
        {
            GameObject.Destroy(roadParent.transform.GetChild(0));
        }

        //添加新路点
        for (var i = 0; i < this.roads.Count; i++)
        {
            Vector2 road = this.roads[i];
            Vector2 roadPos = this.mapCells[(int)road.x, (int)road.y].pos;
            GameObject roadObj = GameObject.Instantiate(roadPrefab,
                new Vector3(roadPos.x, roadPos.y, roadParent.transform.position.z),
                Quaternion.identity);
            roadObj.transform.parent = roadParent.transform;
        }
    }

    public bool canChuBing()
    {
        //判断状态是否可以发兵
        if (status != MapStatus.Inited)
        {
            Debug.Log("地图还未初始化或已经出兵！ "+status);
            return false;
        }
        //判断是否还有可出兵的波数
        if (nowBoShu > data.boshu)
        {
            Debug.Log("已经最后一波了");
            return false;
        }

        return true;
    }

    public void chubing()
    {
        status = MapStatus.ChuBing;
    }
    



}

public enum MapStatus
{
    UnInited,
    Inited,
    ChuBing
}